home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / cdity / yak158src.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-02  |  15.4 KB  |  491 lines

  1. /*
  2.  * Yak version 1.58
  3.  * ----------------
  4.  * [Yak == Yet Another K(?)ommodity
  5.  *
  6.  * There seems to be a profusion of commodities doing this or that.
  7.  * Heres mine, to do what I want it to:
  8.  *
  9.  *      AutoActivate windows (SunMouse)
  10.  *      ClickToFront, ClickToBack, ScreenCycle
  11.  *      Close/Zip/Shrink/Zoom/Turn a window via keyboard.
  12.  *      Bring up a palette on front screen.
  13.  *      Insert date into read-stream.
  14.  *      Produce key-click (like my keyclick program).
  15.  *      Some other things...
  16.  *
  17.  * Martin W. Scott & GaĆ«l Marziou, 8/93.
  18.  */
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <exec/memory.h>
  22. #include <devices/inputevent.h>
  23. #include <dos/dos.h>
  24. #include <dos/dostags.h>
  25. #include <graphics/displayinfo.h>
  26. #include <libraries/commodities.h>
  27. #include <libraries/reqtools.h>
  28. #include <libraries/locale.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <clib/alib_protos.h>
  31. #include <clib/exec_protos.h>
  32. #include <clib/dos_protos.h>
  33. #include <clib/graphics_protos.h>
  34. #include <clib/commodities_protos.h>
  35. #include <clib/intuition_protos.h>
  36. #include <clib/locale_protos.h>
  37. #include <clib/reqtools_protos.h>
  38.  
  39. #ifdef __SASC
  40. #  include <pragmas/exec_pragmas.h>
  41. #  include <pragmas/dos_pragmas.h>
  42. #  include <pragmas/graphics_pragmas.h>
  43. #  include <pragmas/commodities_pragmas.h>
  44. #  include <pragmas/intuition_pragmas.h>
  45. #  include <pragmas/locale_pragmas.h>
  46. #  include <pragmas/reqtools.h>
  47. #else
  48. #  include <proto/exec.h>
  49. #  include <proto/dos.h>
  50. #  include <proto/graphics.h>
  51. #  include <proto/commodities.h>
  52. #  include <proto/intuition.h>
  53. #  include <proto/locale.h>
  54. #  include <proto/reqtools.h>
  55. extern struct Library *DOSBase;
  56. #endif
  57.  
  58.  
  59. #include <string.h>
  60. #include <stdarg.h>
  61.  
  62. #include "code.h"
  63. #include "yak.h"
  64. #include "hotkey_types.h"
  65. #include "beep.h"
  66. #include "icon.h"
  67. #include "version.h"
  68.  
  69. #define CATCOMP_BLOCK
  70. #define CATCOMP_NUMBERS
  71. #include "locale/yak_locale_strings.h"
  72. #undef CATCOMP_BLOCK
  73.  
  74. #include "WB2CLI.h"     /* we'll be a shell process */
  75. #define DEF_CURRENTDIR  "SYS:"
  76.  
  77.  
  78.  
  79. /* local prototypes for main.c */
  80. static void CloseResources(void);
  81. static BOOL OpenResources(void);
  82. static void FreePatterns(void);
  83. static LONG ProcessMsg(void);
  84. void MAIN(void);
  85.  
  86. LONG (*HandleIDCMP)(void);
  87.  
  88. extern struct WBStartup *WBMsg;
  89. /*
  90.  *  libraries opened by startup code; basepointers needed by function pragmas
  91.  */
  92. extern struct Library *SysBase, *DOSBase;
  93.  
  94. /* global data - library bases and the like */
  95. struct Library  *CxBase, *IconBase,
  96.                 *GadToolsBase, *LayersBase,
  97.                 *WorkbenchBase, *LocaleBase,
  98.                 *GfxBase, *IntuitionBase,
  99.                 *KeymapBase;
  100. struct Locale *locale;
  101. struct Catalog *Catalog;
  102. struct MsgPort *broker_mp;
  103. CxObj *broker;
  104. char *PopKeyStr;
  105. #define POPKEY_EVENT    1L      /* cannot clash with YHK event... */
  106.  
  107. static const char *versionstr=VERSION_STR;
  108.  
  109. struct NewBroker newbroker = {
  110.     NB_VERSION,
  111.     "Yak",                                              /* string to identify this broker */
  112.     VERSION_BROKER,
  113.     "Multi-purpose commodity",
  114.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities
  115.                                                                  * starting with this name.  If someone
  116.                                                                  * tries it, let me know */
  117.     COF_SHOW_HIDE
  118.         };
  119.  
  120. ULONG           wndsigflag;             /* here for overlay purposes */
  121. ULONG           cxsigflag;
  122. ULONG           invariantsigflag;
  123. BYTE            palette_count;          /* how many palettes are open */
  124.  
  125. /* from handler.c */
  126. extern ULONG    clicksigflag, intuiopsigflag;
  127. extern void (*intui_routine)(APTR);     /* for intui_op's */
  128. extern APTR intui_parameter;
  129.  
  130.  
  131. /* from icon.c */
  132. extern ULONG    appsigflag;
  133.  
  134. /* close what we opened */
  135. static void
  136. CloseResources()
  137. {
  138.         /* NULL pointers are valide so don't waste time to test them */
  139.         CloseLibrary(IntuitionBase);
  140.         CloseLibrary(GfxBase);
  141.         CloseLibrary(CxBase);
  142.         CloseLibrary(LayersBase);
  143.         CloseLibrary(IconBase);
  144.         CloseLibrary(GadToolsBase);
  145.         CloseLibrary(WorkbenchBase);
  146.         CloseLibrary(KeymapBase);
  147.         if (LocaleBase)
  148.         {
  149.                 CloseCatalog (Catalog); 
  150.                 CloseLocale(locale);
  151.                 CloseLibrary(LocaleBase);
  152.         }
  153. }
  154.  
  155. /* open libraries, devices that we need */
  156. static BOOL
  157. OpenResources(void)
  158. {
  159.         if ((IntuitionBase = OpenLibrary("intuition.library", 37L)) &&
  160.             (GfxBase       = OpenLibrary("graphics.library", 37L)) &&
  161.             (CxBase        = OpenLibrary("commodities.library", 37L)) &&
  162.             (LayersBase    = OpenLibrary("layers.library", 37L)) &&
  163.             (IconBase      = OpenLibrary("icon.library", 37L)) &&
  164.             (GadToolsBase  = OpenLibrary("gadtools.library", 37L)) &&
  165.             (WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
  166.             (KeymapBase    = OpenLibrary("keymap.library", 37L)))
  167.         {
  168.                 return TRUE;
  169.         }
  170.         CloseResources();
  171.         return FALSE;
  172. }
  173.  
  174.  
  175. /* open locale library and catalog */
  176. void
  177. OpenLocaleStuff(char *language)
  178. {
  179.  
  180.         if (LocaleBase =(struct LocaleBase *)OpenLibrary("locale.library", 38L))
  181.         {
  182.                 if (!(locale = OpenLocale(NULL)))
  183.                 {       
  184.                         PostError("No locale set!");
  185.                         CloseLibrary(LocaleBase);
  186.                         LocaleBase = NULL;
  187.                 }
  188.                 Catalog = OpenCatalog(  locale, "yak.catalog", 
  189.                                                           OC_BuiltInLanguage, "english", 
  190.                                                           OC_Language, language,        
  191.                                                           OC_Version, 15L, 
  192.                                                           TAG_DONE );
  193.         }       
  194. }
  195.  
  196.  
  197. /* slighlty modified version of GetString() generated by catcomp */
  198.  
  199. char *getString(ULONG MsgID)
  200. {
  201. LONG   *l;
  202. UWORD  *w;
  203. char *builtIn;
  204.  
  205.    l = (LONG *)CatCompBlock;
  206.  
  207.    while (*l != MsgID )
  208.    {
  209.            w = (UWORD *)((ULONG)l + 4);
  210.            l = (LONG *)((ULONG)l + (ULONG)*w + 6);
  211.    }
  212.    builtIn = (char *)((ULONG)l + 6);
  213.    if (LocaleBase)
  214.            return (GetCatalogStr (Catalog, MsgID, builtIn));
  215.    return(builtIn);
  216. }
  217.  
  218. /* simple requester with args */
  219. void
  220. PostError(char *body, ... )
  221. {
  222.         struct EasyStruct es;
  223.         va_list args;
  224.  
  225.         if (!IntuitionBase)
  226.         {
  227.                 Write(Output(), "Need AmigaDos 2.0+\n", -1);
  228.                 return;
  229.         }
  230.  
  231.         /* setup the argument array */
  232.         va_start( args, body );
  233.  
  234.         /* initialise the structure */
  235.         es.es_StructSize = sizeof(struct EasyStruct);
  236.         es.es_Flags = 0L;
  237.         es.es_Title = getString(Error_Requester_Title);
  238.         es.es_TextFormat = body;
  239.         es.es_GadgetFormat = "OK";
  240.  
  241.         /* display the requester */
  242.         EasyRequestArgs(NULL, &es, NULL, args);
  243.  
  244.         /* free the arguments */
  245.         va_end( args );
  246. }
  247.  
  248. /* parse pattern, report errors */
  249. __regargs BOOL
  250. InitPattern(char *newpatstr, UWORD n)
  251. {
  252.         char *patstr = newpatstr ? newpatstr : patterns[n].patstr;
  253.         char *pat;
  254.         LONG len;
  255.  
  256.         if (pat = AllocVec(len = strlen(patstr)*3+10, MEMF_CLEAR))
  257.         {
  258.                 if (ParsePattern(patstr, pat, len) != -1)
  259.                 {
  260.                         if (newpatstr) strncpy(patterns[n].patstr, newpatstr, PATLEN);
  261.                         if (patterns[n].pat) FreeVec(patterns[n].pat);
  262.                         patterns[n].pat = pat;
  263.                         return TRUE;
  264.                 }
  265.                 
  266.                 PostError("%s:\n\"%s\"", getString(Parsing_Pattern_ERR), patstr);
  267.                 FreeVec(pat);
  268.         }
  269.         else PostError(getString(Allocation_ERR));
  270.         return FALSE;
  271. }
  272.  
  273. static void
  274. FreePatterns()
  275. {
  276.         UWORD i;
  277.  
  278.         for (i = 0; i < NUM_PATTERNS; i++)
  279.                 if (patterns[i].pat) FreeVec(patterns[i].pat);
  280. }
  281.  
  282.  
  283. #ifdef _DCC
  284. void _waitwbmsg(void);
  285. static void
  286. uncalled(void)
  287. {
  288.     _waitwbmsg();
  289. }
  290. #endif
  291.  
  292.  
  293.  
  294. void MAIN()             /* Yak: multi-function commodity */
  295. {
  296.         BPTR    newdir = NULL, olddir;
  297.  
  298.         if (OpenResources())
  299.         {
  300.             if (broker_mp = CreateMsgPort())
  301.                 {
  302.                         newbroker.nb_Port = broker_mp;
  303.                         cxsigflag = 1L << broker_mp->mp_SigBit;
  304.  
  305.                         /* process tool-types */
  306.                         GetOurIcon(WBMsg);
  307.                         newbroker.nb_Pri = (BYTE)TTInt("CX_PRIORITY", 0);
  308.  
  309.                         if (WBMsg)
  310.                         {
  311.                                 if (newdir = Lock(DEF_CURRENTDIR, ACCESS_READ))
  312.                                         olddir = CurrentDir(newdir);
  313.                                 WB2CLI(WBMsg,4000,(struct DosLibrary *)DOSBase);   /* get it over with... */
  314.                         }
  315.  
  316.                         if (broker = CxBroker(&newbroker, NULL))
  317.                         {
  318.                                 /* HANDLER FIRST, SO IT SEES EVERYTHING!!! */
  319.                                 if (InitHandler())
  320.                                 {       
  321.                                         InitYakHotKeyList();
  322.                                         LoadSettings();
  323.                                         
  324.                                         /* Open the right locale if tooltype LANGUAGE is used */
  325.                                         OpenLocaleStuff(TTString("LANGUAGE",NULL));
  326.  
  327.                                         if (PopKeyStr = DupStr(TTString("CX_POPKEY", "Rcommand help")))
  328.                                         {
  329.                                                 CxObj *tmpobj;
  330.                                         
  331.                                                 if (tmpobj = HotKey(PopKeyStr, broker_mp, POPKEY_EVENT))
  332.                                                         AttachCxObj(broker, tmpobj);
  333.                                                 else
  334.                                                         PostError("%s:\"%s\"", getString(CX_POPKEY_invalid_ERR), 
  335.                                                                           PopKeyStr);
  336.                                         }
  337.                                         /* else... if this failed, we lose */
  338.  
  339.                                         MyPri(ACTIVE);
  340.                                         ActivateCxObj(broker, 1L);
  341.  
  342.                                         if (TTBool("CX_POPUP", FALSE))
  343.                                                 ShowYakInterface();
  344.  
  345.                                         if (TTBool("APPICON", FALSE))
  346.                                         {
  347.                                                 if (!MakeOurAppIcon(TTString("ICONNAME", "Yak!")))
  348.                                                         if (WBMsg)
  349.                                                                 PostError(getString(Couldn_t_create_AppIcon_ERR));
  350.                                         }
  351.                                         /* WB makes a copy of icon, so can free it */
  352.                                         FreeOurIcon();
  353.  
  354.                                         /* these are the signals waited for, + window sig */
  355.                                         invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag
  356.                                                 | clicksigflag | intuiopsigflag
  357.                                                         | appsigflag;
  358.  
  359.                                         while (ProcessMsg())
  360.                                                 ;
  361.                                         HideInterface();
  362.                                         RemoveOurAppIcon();
  363.                                         DeleteYakHotKeyList();
  364.  
  365.                                         FreeStr(PopKeyStr);
  366.                                         MyPri(ORIGINAL);
  367.  
  368.                                         EndHandler();
  369.                                         FreePatterns();
  370.                                 }
  371.                                 else 
  372.                                         PostError(getString(Allocation_ERR));
  373.         
  374.                                 DeleteCxObjAll(broker);
  375.                         }
  376.  
  377.                         if (newdir) 
  378.                         {
  379.                                 CurrentDir(olddir);
  380.                                 UnLock(newdir);
  381.                         }
  382.                         DeleteMsgPort(broker_mp);
  383.                         FreeOurIcon();  /* may already be gone, but so what? */
  384.                 }
  385.             else PostError(getString(Allocation_ERR));
  386.  
  387.                 CloseResources();
  388.                 MWReport("At end of main()", MWR_FULL);  /* Generate a memory usage report */
  389.         }
  390.         else PostError(getString(Resource_ERR));
  391. }
  392.  
  393.  
  394.  
  395.  
  396. /* monitor cx port, act on messages */
  397. static LONG
  398. ProcessMsg(void)
  399. {
  400.     CxMsg *msg;
  401.     ULONG sigrcvd, msgid, msgtype;
  402.     LONG returnvalue = 1L;
  403.  
  404.     sigrcvd = Wait(invariantsigflag | wndsigflag);
  405.  
  406.     if (sigrcvd & intuiopsigflag) 
  407.         {       
  408.                 /* intuiop requested */
  409.                 intui_routine(intui_parameter);
  410.         }
  411.  
  412.  
  413.     if ((sigrcvd & clicksigflag) && click_volume)       /* keyclick please */
  414.     {
  415.                 beep(click_volume);
  416.             Delay(1);   /* avoid ugly sound when key repeating */
  417.     }
  418.  
  419.     if (sigrcvd & appsigflag)           /* settings change */
  420.     {
  421.                 RespondToAppIcon();
  422.                 ShowYakInterface();
  423.     }
  424.  
  425.     if (sigrcvd & wndsigflag)           /* settings change */
  426.         if ((*HandleIDCMP)() != ROOT_OKAY)
  427.                         returnvalue = 0;
  428.         else 
  429.                 if (!wndsigflag)                /* window gone */
  430.                         DummyOverlay(); 
  431.         /* remove code */
  432.  
  433.     while(msg = (CxMsg *)GetMsg(broker_mp))
  434.     {
  435.         msgid = CxMsgID(msg);
  436.         msgtype = CxMsgType(msg);
  437.         ReplyMsg((struct Message *)msg);
  438.  
  439.         switch(msgtype)
  440.         {
  441.                   case CXM_IEVENT:
  442.  
  443.                         if (msgid == POPKEY_EVENT)
  444.                                 ShowYakInterface();
  445.                         else
  446.                                 /* a generic hotkey... */
  447.                                 PerformAction((YakHotKey *)msgid);
  448.                         break;
  449.  
  450.             case CXM_COMMAND:
  451.                 switch(msgid)
  452.                 {
  453.                                   case CXCMD_UNIQUE:
  454.                                   case CXCMD_APPEAR:
  455.                                         ShowYakInterface();     /* check error return? */
  456.                                         break;
  457.  
  458.                                   case CXCMD_DISAPPEAR:
  459.                                         HideInterface();
  460.                                         DummyOverlay();
  461.                                         break;
  462.  
  463.                                   case CXCMD_DISABLE:
  464.                                         ActivateCxObj(broker, 0L);
  465.                                         TurnMouseOn();
  466.                                         break;
  467.  
  468.                                   case CXCMD_ENABLE:
  469.                                         ActivateCxObj(broker, 1L);
  470.                                         break;
  471.  
  472.                                   case CXCMD_KILL:
  473.                                         returnvalue = 0L;
  474.                                         break;
  475.                 }
  476.                 break;
  477.         }
  478.     }
  479.  
  480.     if (sigrcvd & SIGBREAKF_CTRL_C)
  481.         returnvalue = 0L;
  482.  
  483.     if (!returnvalue && !OkayToExit())
  484.     {
  485.         PostError(getString(Cannot_exit_palette_opened_ERR));
  486.         returnvalue = 1;
  487.     }
  488.     
  489.     return(returnvalue);
  490. }
  491.